<?php
//后台用户管理
namespace App\Http\Controllers\Admin;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Mail\Message;

class UserController extends BaseController
{

    //用户列表
    public function index(){
        //分页
        $data = User::orderby('id','desc')
            ->withTrashed()  //包括软删除的数记录
            ->paginate($this->pagesize);
        return view('admin.user.index',compact('data'));
    }

    //添加用户-显示
    public function create(){
        return view('admin.user.create');
    }

    //添加用户-处理
    public function store(Request $request){
        $this->validate($request,[
            'username'=>'required|unique:users,username',
            'truename'=>'required',
            'password'=>'required|confirmed',
            'phone'=>'nullable|phone',
        ],[
            'truename.required'=>'姓名 不能为空',
        ]);

        //添加用户入库
        $post = $request->except(['_token','password_confirmation']);
        $post['password'] = bcrypt($post['password']);
        $userModel = User::create($post);

        $pwd = $post['password'];
        //发邮件
        //参数1 模板视图
        //参数2 传递给模板的变量
        \Mail::send('admin.mail.useradd',compact('userModel','pwd'),function(Message $message) use ($userModel) {
            //发给谁
            $message->to($userModel->email);
            //主题
            $message->subject('开通账号通知');
        });

        //跳转到列表页
        return redirect(route('admin.user.index'))->with(['success'=>'用户添加成功']);
    }

    //删除用户
    public function del(int $id){
        //删除
        //由于在User的Model中设置了软删除,因此这里不会真正删除
        $result = User::find($id)->delete();
        //强制删除 在配置了软删除后,真实的删除操作
        //User::find($id)->forceDelete();

        if($result){
            $res = ['status'=>1,'msg'=>'删除成功'];
            return $res;
        }else{
            $res = ['error'=>'操作失败'];
            return redirect(route('admin.user.index'))->withErrors($res);
        }
        return json_encode($res);
    }

    //恢复软删除
    public function restore(int $id){
        //还原
        $result = User::onlyTrashed()->where('id',$id)->restore();
        if($result){
            $res = ['success'=>'操作成功'];
            return redirect(route('admin.user.index'))->with($res);
        }else{
            $res = ['error'=>'操作失败'];
            return redirect(route('admin.user.index'))->withErrors($res);
        }
    }

    //全选删除
    public function delall(Request $request){
        $ids = $request->get('id');
        User::destroy($ids);
        return json_encode(['status'=>1,'msg'=>'批量删除成功']);
    }

    //编辑显示
    public function edit(int $id){
        $model = User::find($id);

        return view('admin.user.edit',compact('model'));
    }

    //编辑处理
    public function update(Request $request,int $id){
        $this->validate($request,[
            'truename'=>'required',
            'password'=>'confirmed',
            'phone'=>'nullable|phone',
        ],[
            'truename.required'=>'姓名 不能为空',
        ]);

        $model = User::find($id);

        //用户输入原密码
        $spassword = $request->get('spassword');
        //原密码 密文
        $old_pass = $model->password;

        //检查明文和密文是否一致
        $bool = \Hash::check($spassword,$old_pass);

        if($bool == false){
            return redirect(route('admin.user.edit',$model))->withErrors(['error'=>'原密码不正确']);
        }

        //修改
        $data = $request->only([
            'truename',
            'password',
            'sex',
            'phone',
            'email'
        ]);
        if(!empty($data['password'])){
            $data['password'] = bcrypt($data['password']);
        }else{
            unset($data['password']);
        }

        $model->update($data);
        return redirect(route('admin.user.index'))->with(['success'=>'修改用户信息成功']);
    }
}
